package com.feiyisys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.feiyisys.common.PageResult;
import com.feiyisys.common.Result;
import com.feiyisys.dto.ActivityDTO;
import com.feiyisys.dto.ActivityResponseDTO;
import com.feiyisys.entity.Activity;
import com.feiyisys.service.ActivityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.feiyisys.utils.TencentCosUtil;
import lombok.extern.slf4j.Slf4j;
import jakarta.annotation.Resource;

/**
 * <p>
 * 文化遗产相关活动表 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/activity")
@CrossOrigin("*")
@Tag(name = "活动管理")
@Slf4j
public class ActivityController {
    @Resource
    private ActivityService activityService;
    
    @Resource
    private TencentCosUtil tencentCosUtil;

    /**
     * 获取活动列表（分页）
     *
     * @param current 当前页码
     * @param size    每页大小
     * @return 分页结果
     */
    @Operation(summary = "获取活动列表")
    @GetMapping("/page")
    public PageResult<ActivityResponseDTO> getActivityPage(
            @RequestParam(defaultValue = "1") Long current,
            @RequestParam(defaultValue = "10") Long size) {
        try {
            IPage<ActivityResponseDTO> page = activityService.getActivityPage(current, size);
            return PageResult.success(page);
        } catch (Exception e) {
            return PageResult.error("获取活动列表失败");
        }
    }

    /**
     * 根据ID获取活动详情
     *
     * @param id 活动ID
     * @return 活动详情
     */
    @Operation(summary = "获取活动详情")
    @GetMapping("/detail/{id}")
    public Result<ActivityResponseDTO> getActivityById(@PathVariable Long id) {
        try {
            ActivityResponseDTO activity = activityService.getActivityById(id);
            return Result.success(activity);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 创建活动
     *
     * @param activityDTO 活动信息
     * @return 创建结果
     */
    @PostMapping("/create")
    public Result<ActivityResponseDTO> createActivity(
            @RequestBody ActivityDTO activityDTO) {
        try {
            Activity activity = activityService.createActivity(activityDTO);
            // 调用service层获取响应DTO，确保返回格式与其他API一致
            ActivityResponseDTO responseDTO = activityService.getActivityById(activity.getId());
            return Result.success(responseDTO);
        } catch (Exception e) {
            return Result.error("创建活动失败：" + e.getMessage());
        }
    }
    /**
     * 删除活动
     *
     * @param id 活动ID
     * @return 删除结果
     */
    @Operation(summary = "删除活动")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteActivity(@PathVariable Long id) {
        try {
            boolean result = activityService.removeById(id);
            if (result) {
                return Result.success(true);
            } else {
                return Result.error("删除失败，活动不存在");
            }
        } catch (Exception e) {
            return Result.error("删除活动失败：" + e.getMessage());
        }
    }

    /**
     * 更新活动
     *
     * @param activityDTO 活动信息
     * @return 更新结果
     */
    @Operation(summary = "更新活动")
    @PutMapping("/update")
    public Result<Activity> updateActivity(@RequestBody ActivityDTO activityDTO) {
        try {
            Activity activity = activityService.updateActivity(activityDTO);
            return Result.success(activity);
        } catch (Exception e) {
            return Result.error("更新活动失败：" + e.getMessage());
        }
    }

    /**
     * 更新活动状态
     *
     * @param id     活动ID
     * @param status 状态
     * @return 更新结果
     */
    @Operation(summary = "更新活动状态")
    @PutMapping("/status/{id}")
    public Result<Boolean> updateActivityStatus(@PathVariable Long id, @RequestParam Byte status) {
        try {
            boolean result = activityService.updateActivityStatus(id, status);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("更新活动状态失败：" + e.getMessage());
        }
    }
    
    @PostMapping("/upload")
    public Result<String> uploadPicture(@RequestParam("file") MultipartFile file) {
        try {
            // 上传文件到腾讯云COS
            String key = tencentCosUtil.uploadFile(file);
            // 获取文件完整访问URL
            String fileUrl = tencentCosUtil.getFileUrl(key);
            log.info("文件上传成功, URL: {}", fileUrl);
            return Result.success(fileUrl);
        } catch (Exception e) {
            log.error("文件上传失败: {}", e.getMessage());
            return Result.error("文件上传失败: " + e.getMessage());
        }
    }
}
